package com.whfp.coky.ui.task.vm.rfid

import android.graphics.Bitmap
import androidx.lifecycle.viewModelScope
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.vm.RFIDVM
import com.whfp.coky.ui.task.model.PrintCodeBean
import com.whfp.coky.utils.RFIDTool
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *
 *
 * created by coky on 2022/3/1
 */
class ReadVM: RFIDVM() {

    //    读卡，获取卡片信息展示
    override fun doUpdate(activity: BaseActivity<*, *>) {//generateQRcode()
        if (currentRssiMax == null) {
            activity.toast("需要生成二维码的内容为空！")
            return
        }
        //异步执行生成二维码
        activity.showDialog()
        content = RFIDTool.hexStr2str(currentRssiMax?.getId()!!)

        PrintCodeBean().apply {
            this.title = "读卡名称"
            this.content = "读卡内容"
            var qrBitmap: Bitmap? = null
           viewModelScope.launch {
                qrBitmap = QRCodeEncoder.syncEncodeQRCode(content, 420)
                while (qrBitmap == null){
                    delay(300L)
                }
            }
//            ?????????????仅填入bitmap为何需要PrintCodeBean类？？？
            this.bitmap = qrBitmap
        }.let {
            activity.hideDialog()
            inputQrCode(it)
        }
    }

    private fun inputQrCode(result: PrintCodeBean) {//填入二维码
        vBinding.rfidItem.ivQrCode.setImageBitmap(result.bitmap)
    }



}